package org.me.marble;

import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;

/**
 *
 * @author tungi
 */

public class OpenGLSystem{
    //there is the contants the arg is created with
    //the first three use with enable/disable method, the next threes
    //enableClientState/disableClientState
    public static final int CULL_FACE=1;
    public static final int BLEND=2;
    public static final int TEXTURE=4;

    public static final int TEXTURE_ARRAY=8;
    public static final int NORMAL_ARRAY=16;
    public static final int VERTEX_ARRAY=32;

    public static final int BLEND_IF_NO_FOG=64;

    public static boolean _texture=false,_texture_array=false,_normal=false;

    public static void enable(GL10 gl,int arg){
        //arg is a number in binary representation like: 101
        //we dividing a number with two, if the residuum is not zero
        //there is a one on the least place
        //then we shift the number right (the number 101 will be 10),
        //and begin the test afresh
        if(arg%2!=0){
            gl.glEnable(GL10.GL_CULL_FACE);
            gl.glFrontFace(GL10.GL_CW);
        }
        if(((arg>>1)%2!=0 || ((arg>>6)%2!=0 && !OptionsLibrary.fog)) &&
                OptionsLibrary.graphics_quality.equals("high")){
            gl.glEnable(GL10.GL_BLEND);
            //gl.glBlendFunc(GL10.GL_ONE,GL10.GL_SRC_COLOR);
            gl.glBlendFunc(GL10.GL_SRC_ALPHA,GL10.GL_ONE_MINUS_SRC_ALPHA);
        }
        if((arg>>2)%2!=0 && (OptionsLibrary.graphics_quality.equals("medium")
                ||  OptionsLibrary.graphics_quality.equals("high")) && !_texture){
            //gl.glEnable(GL10.GL_TEXTURE);
            gl.glEnable(GL10.GL_TEXTURE_2D);
            _texture=true;
        }
    }

    public static void disable(GL10 gl,int arg){
        if((arg>>2)%2!=0 && (OptionsLibrary.graphics_quality.equals("medium")
                ||  OptionsLibrary.graphics_quality.equals("high")) && _texture){
            gl.glDisable(GL10.GL_TEXTURE_2D);
            //gl.glDisable(GL10.GL_TEXTURE);
            _texture=false;
        }
        if((arg>>1)%2!=0 && OptionsLibrary.graphics_quality.equals("high"))
            gl.glDisable(GL10.GL_BLEND);
        if(arg%2!=0)
            gl.glDisable(GL10.GL_CULL_FACE);
    }

    public static void enableClientStates(GL10 gl,int arg){
        if((arg>>5)%2!=0)
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        if((arg>>4)%2!=0){
            _normal=true;
            gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
        }
        if((arg>>3)%2!=0 && (OptionsLibrary.graphics_quality.equals("medium")
                ||  OptionsLibrary.graphics_quality.equals("high")) && !_texture_array){
            _texture_array=true;
            gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        }
    }

    public static void disableClientStates(GL10 gl,int arg){
        if((arg>>3)%2!=0 && _texture_array){
            _texture_array=false;
            gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        }
        if((arg>>4)%2!=0){
            _normal=false;
            gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
        }
        if((arg>>5)%2!=0)
            gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    }

    public static void drawTriangles(GL10 gl,Texture texture,FloatBuffer points,
            FloatBuffer normals,FloatBuffer textures,int num,int type){
        gl.glVertexPointer(3,GL10.GL_FLOAT,0,points);
        if(_normal)
            gl.glNormalPointer(GL10.GL_FLOAT,0,normals);
        if(_texture && _texture_array){
            gl.glTexCoordPointer(2,GL10.GL_FLOAT,0,textures);
            gl.glBindTexture(texture.getType(),texture.getName());
        }
        gl.glDrawArrays(type,0,num);
    }

    public static void setMaterial(GL10 gl,MyColor color){
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK,GL10.GL_AMBIENT,color.getAmbient(),0);
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK,GL10.GL_DIFFUSE,color.getDiffuse(),0);
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK,GL10.GL_SPECULAR,color.getSpecular(),0);
        gl.glMaterialfv(GL10.GL_FRONT_AND_BACK,GL10.GL_EMISSION,color.getEmission(),0);
        gl.glMaterialf(GL10.GL_FRONT_AND_BACK,GL10.GL_SHININESS,color.getShininess());
    }
}
